<template>
  <div class="movies">
    <div
      class="movies-item"
      v-for="(item, index) in moviesList"
      :key="index"
      @click="goToDetail(item.movieid)"
    >
      <img :src="item.img | formatUrl" alt="" class="logo" />
      <!-- 过滤器：文本格式化 -->
      <!-- https://p1.meituan.net/w.h/movie/13cb85d810b0530b951625430c2f35044951112.jpg -->

      <h4>{{ item.nm }}</h4>
    </div>
  </div>
</template>

<script>
import { moviesApi, getMoreApi } from "@/utils/api";
export default {
  data() {
    return {
      moviesList: [],
      count: 0,
      ids: [],
      // 表示从哪个索引开始请求
      startIndex: 0,
      // 每次获取几条
      size: 6,
    };
  },
  mounted() {
    this.getMoviesList();
  },
  methods: {
    async getMoviesList() {
      const res = await moviesApi();
      this.moviesList = res.result;
      this.count = res.count; // 33
      this.ids = res.ids;
      this.startIndex = res.result.length;

      await this.$nextTick();

      // 让父组件去添加bs
      this.$emit("finish");
    },
    async getMore() {
      //todo 截取ids的id
      const arr = this.ids.slice(this.startIndex, this.startIndex + this.size);
      // console.log(arr);
      const str = arr.join(",");

      const res = await getMoreApi({ ids: str });
      // console.log(res);
      this.moviesList = [...this.moviesList, ...res.result];
      this.startIndex += this.size;

      await this.$nextTick();

      this.$emit("finishmore", this.moviesList.length >= this.count);
    },
    goToDetail(id) {
      this.$router.push("/detail/" + id);
    },
  },
  filters: {
    formatUrl(val) {
      return val.split("/w.h").join("/120.200");
    },
  },
};
</script>

<style lang="less" scoped>
@import "../../../../../assets/css/var.less";
.movies {
  background: #fff;
  padding-left: 15px;

  .movies-item {
    width: 100%;
    height: 114px;
    border-bottom: 1px solid @border-color;
    display: flex;

    .logo {
      width: 64px;
      height: 95px;
      margin: 12px 14px 12px 0;
    }
  }
}
</style>
